package zsw.proxy.gdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ZhangHao implements Person{

	@Override
	public String getName(String name) {
		System.out.println("我的名字叫："+name);
		return "hello"+name;
	}

	@Override
	public void run() {
		System.out.println("我学会了跑步");
		
	}
	
	public static void main(String[] args) {
		Handler f=new Handler();
		Person p=new ZhangHao();
		Person ff=(Person) f.getInstances(p);
		String name = ff.getName("张浩");
		System.out.println(name);
		System.out.println(ff);
	}


}

class Handler implements InvocationHandler{
	
	private Object obj;
	
	public Object getInstances(Object obj) {
		this.obj=obj;
		Object newProxyInstance = Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), this);
		return newProxyInstance; 	
		}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		System.out.println(method.getName()+"已经进入gdk的动态代理，即将执行方法");
		Object invoke = method.invoke(obj, args);
		System.out.println(method.getName()+"gdk的动态代理结束，方法执行结束");
		return invoke;
	}
	
	
	
}



